<template>
  <!-- 登录组件的根节点 -->
  <div class="login-container">
    <!-- 登录盒子区域 -->
    <div class="login-box">
      <!--  描述    -->
      <div class="login-header" style="width: auto; height: auto;">
        <p style="text-align: center; font-size: 22px;">应用审计平台</p>
      </div>
      <!-- 登录表单区域 -->
      <el-form label-width="0px" :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-row :gutter="0">
          <el-col :span="16">
            <el-form-item prop="captcha">
              <el-input  prefix-icon="iconfont icon-yanzhengma" v-model="loginForm.captcha" size="large" type="text" placeholder="请输入验证码">
                <el-icon slot="prefix"  :style="{ color: 'rgba(0,0,0,.25)' }">
                </el-icon>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="text-align: right">
            <img v-if="requestCodeSuccess" style="margin-top: 2px;" :src="randCodeImage"
                 @click="handleChangeCheckCode"/>
            <img v-else style="margin-top: 2px;" src="../assets/checkcode.png" @click="handleChangeCheckCode"/>
          </el-col>
        </el-row>
        <!-- 按钮区域 -->
        <el-form-item class="btn-box">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      identifyCodes: "1234567890ABCDEF",
      identifyCode: "",
      // 登录的表单数据对象
      loginForm: {
        username: 'audit-admin',
        password: '',
        captcha: '',
        captchaKey: ''
      },
      randCodeImage: '',
      requestCodeSuccess: false,
      // 登录表单的验证规则对象
      loginFormRules: {
        username: [
          {
            required: true,
            message: '请输入登录名称',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入登录密码',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 10,
            message: '密码长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        captcha: [{
          required: true, message: '请输入验证码!'
        }],
      }
    }
  },
  // 默认启动时就调用
  mounted() {
    this.handleChangeCheckCode()
  },
  methods: {
    // 重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    /**刷新验证码*/
    handleChangeCheckCode() {
      let nowTime = new Date().getTime()
      console.log("start call randomImage")
      axios.get(`/sys/randomImage/${nowTime}`).then(res => {
        if (res.data.success) {
          this.randCodeImage = res.data.result
          this.requestCodeSuccess = true
        } else {
          this.$message.error(res.data.message)
          this.requestCodeSuccess = false
        }
      }).catch(() => {
        this.requestCodeSuccess = false
      })
    },
    login() {
      // 调用表单验证的 API 函数
      this.$refs.loginFormRef.validate(async valid => {
        // valid 为 true，表示验证通过；false 表示验证不通过
        if (!valid) return
        // 发起请求
        this.loginForm.captchaKey = new Date().getTime()
        const {data: res} = await this.$http.post('/sys/login', this.loginForm)
        console.log(res)
        if (res.code !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功！')
        // 将 token 存储到 sessionStorage 中
        window.sessionStorage.setItem('token', res.token)
        // 通过编程式导航跳转到 /home
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background: #f0f2f5 url(~@/assets/background.svg) no-repeat 50%;
  height: 100%;

  // 美化登录盒子的样式
  .login-box {
    width: 390px;
    height: 330px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px #dddddd;
  }
}

.btn-box {
  display: flex;
  justify-content: flex-end;
}

.el-form {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }

  .el-col {
    border-radius: 4px;
  }
}
</style>
